package com.sdp.right.service;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sdp.core.bizc.threadLocal.ContextManager;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.right.db.sys_emp.service.ISysEmpService;
import com.sdp.right.db.sys_org.service.ISysOrgService;
import com.sdp.right.db.sys_role.service.ISysRoleService;
import com.sdp.right.db.sys_role_person.service.ISysRolePersonService;
import com.sdp.right.entity.SysEmp;
import com.sdp.right.entity.SysOrg;
import com.sdp.right.entity.SysRole;
import com.sdp.right.utils.RightConstants;



@DS(value="right")
@Component
public class RightExtService {
	
	@Autowired
	private ISysEmpService iSysEmpService;
	
	@Autowired
	private ISysRolePersonService iSysRolePersonService;
	
	@Autowired
	private ISysOrgService iSysOrgService;
	
	@Autowired
	private ISysRoleService iSysRoleService;
	
	
	/**
	 * 获取当前登录人信息
	 * @return
	 */
	@DS(value="right")
	public SysEmp getCurrentEmpInfo() {
		return iSysEmpService.querySysEmpByAccountNo(ContextManager.getContextData(CoreConstantsEnum.token中账号的属性名.getCode()));
	}
	
	/**
	 * 通过工号更新权限人员信息
	 * @param empNo
	 * @param sysEmp
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void updateSysEmpByEmpNo(String empNo,SysEmp sysEmp) {
		SysEmp emp = iSysEmpService.querySysEmpByEmpNo(empNo);
		BeanUtils.copyProperties(sysEmp, emp);
		LambdaUpdateWrapper<SysEmp> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(SysEmp::getEmpNo, empNo);
		iSysEmpService.update(emp, updateWrapper);
	}
	
	/**
	 * 通过账号更新权限人员信息
	 * @param empNo
	 * @param sysEmp
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void updateSysEmpByAccountNo(String accountNo,SysEmp sysEmp) {
		SysEmp emp = iSysEmpService.querySysEmpByAccountNo(accountNo);
		BeanUtils.copyProperties(sysEmp, emp);
		LambdaUpdateWrapper<SysEmp> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(SysEmp::getAccountNo, accountNo);
		iSysEmpService.update(emp,updateWrapper);
	}
	
	/**
	 * 添加账号
	 * @param sysEmp
	 * @throws NoSuchAlgorithmException
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void addSysEmp(SysEmp sysEmp) throws NoSuchAlgorithmException{
		
		if(StringUtils.isEmpty(sysEmp.getEmpPwd())) {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(RightConstants.默认密码.getCode().getBytes());
			sysEmp.setEmpPwd(new BigInteger(1, md.digest()).toString(16));
		}
        
        sysEmp.setCreateTime(LocalDateTime.now());
        sysEmp.setIsUse(RightConstants.是.getCode());
        iSysEmpService.save(sysEmp);
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void addSysEmpBatch(List<SysEmp> sysEmpList) {
		if (sysEmpList != null && sysEmpList.size() > 0) {
			try {
				for (SysEmp sysEmp : sysEmpList) {
					if(StringUtils.isEmpty(sysEmp.getEmpPwd())) {
						MessageDigest md = MessageDigest.getInstance("MD5");
						md.update(RightConstants.默认密码.getCode().getBytes());
						sysEmp.setEmpPwd(new BigInteger(1, md.digest()).toString(16));
					}
					sysEmp.setCreateTime(LocalDateTime.now());
			        sysEmp.setIsUse(RightConstants.是.getCode());
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			
			iSysEmpService.saveBatch(sysEmpList);
		}
		
		
	}
	
	/**
	 * 添加账号
	 * @param sysEmp
	 * @throws NoSuchAlgorithmException
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void addSysEmp(SysEmp sysEmp,String roleNo) throws NoSuchAlgorithmException{
		addSysEmp(sysEmp);
        iSysRolePersonService.addRolePerson(sysEmp, roleNo);
        
	}
	
	/**
	 * 根据账号删除权限人员
	 * @param accountNo
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void removeSysEmpByaccountNo(String accountNo){
		//删除人员
		iSysEmpService.removeSysEmpByAccountNo(accountNo);
		
		//删除人员角色
		iSysRolePersonService.removeSysRolePersonByAccountNo(accountNo);
		
	}
	
	/**
	 *添加权限系统中的组织信息
	 * @param sysOrg
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void addSysOrg(SysOrg sysOrg) {
		iSysOrgService.save(sysOrg);
	}
	
	/**
	 * 更新权限系统中的组织信息
	 * @param sysOrg
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void updateSysOrg(SysOrg sysOrg) {
		iSysOrgService.updateById(sysOrg);
	}
	
	/**
	 * 根据组织编码更新组织信息
	 * @param empNo
	 * @param sysEmp
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void updateSysOrgByNo(String orgNo,SysOrg sysOrg) {
		LambdaUpdateWrapper<SysOrg> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(SysOrg::getOrgNo, orgNo);
		iSysOrgService.update(updateWrapper);
	}
	
	/**
	 * 根据组织编号删除权限组织信息
	 * @param orgNo
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void delSysOrgByNo(String orgNo) {
		iSysOrgService.removeSysOrgByNo(orgNo);
	}
	
	/**
	 * 删除角色
	 * @param roleNo
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	@DS(value="right")
	public void delSysRoleByNo(String roleNo) {
		iSysRoleService.removeSysRole(roleNo);
	}
	
	/**
	 * 根据账号获所属的角色编号信息
	 * @param accountNo
	 * @return
	 */
	@DS(value="right")
	public List<String> getRoleNosByAccountNo(String accountNo) {
		return iSysRolePersonService.queryRoleNos(accountNo);
	}
	
	/**
	 * 根据账号获取所属角色信息
	 * @param accountNo
	 * @return
	 */
	@DS(value="right")
	public List<SysRole> getRolesByAccountNo(String accountNo) {
		List<String> roleNoList = iSysRolePersonService.queryRoleNos(accountNo);
		if(roleNoList!=null && roleNoList.size()>0) {
			return iSysRoleService.querySysRole(roleNoList);
		}
		return null;
		
	}
	
}
